﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PinLayoutMVC.Core;
using PinLayoutMVC.Models;

namespace PinLayoutMVC.Controllers
{
    public class SurveyController : Controller
    {
        //
        // GET: /Survey/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult TakeSurvey(int? surveyId, int? questionId)
        {
            //TODO get the actual survey from database
            SurveyModel survey = DummyData.GetData().Surveys.Where(s => s.ID == surveyId.Value).FirstOrDefault();


            // Get Question
            QuestionModel question;
            if (questionId.HasValue)
            {
                question = (from q in survey.Questions where q.ID == questionId select q).First();
            }
            else
            {
                question = survey.Questions.First();
                questionId = question.ID;
            }

            ViewData["surveyId"] = surveyId;
            ViewData["questionId"] = questionId;
            return View(question);
        }

        [HttpPost]
        public ActionResult TakeSurvey(int surveyId, int questionId, int? answerId, string customAnswer)
        {
            //TODO get the actual survey from database
            SurveyModel survey = DummyData.GetData().Surveys.Where(s => s.ID == surveyId).FirstOrDefault();

            // Get Question
            QuestionModel question = (from q in survey.Questions where q.ID == questionId select q).First();
            // Process Answer
            int nextQuestionId;
            if (question.Type.Equals(QuestionModel.EQuestionType.open))
            {
                //TODO store the answer

                var nextQuestions = (from q in survey.Questions where q.Number > question.Number select q.Number);
                if (nextQuestions.Count() > 0)
                {
                    nextQuestionId = nextQuestions.First();
                }
                else
                {
                    nextQuestionId = 0;
                }
            }
            else
            {
                AnswerModel answer = (from a in question.Answers where a.ID == answerId select a).First();
                //TODO store the answer
                nextQuestionId = answer.LeadsToQuestion;
            }

            //Get Next Question
            if (nextQuestionId != 0)
            {
                QuestionModel nextQuestion = (from q in survey.Questions where q.Number == nextQuestionId select q).First();
                return RedirectToAction("TakeSurvey", new { @surveyId = surveyId, @questionId = nextQuestion.ID });
            }
            else
            {
                //TODO go to end
                return RedirectToAction("EndOfSurvey");
            }
        }

        public ActionResult EndOfSurvey()
        {
            return View();
        }

    }
}